package com.lmx.wuzhangshao.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.lmx.wuzhangshao.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Leung
 * @date 2020/8/10 17:47
 */
public class BannerPager extends RelativeLayout implements View.OnClickListener {

    /**
     * 上下文
     */
    private Context mContext;
    private ViewPager mViewPager;
    /**
     * 图片列表
     */
    private List<ImageView> mViewList = new ArrayList<>();
    /**
     * 横幅点击事件
     */
    private BannerClickListener mListener;
    /**
     * 时间间隔，默认2秒
     */
    private int mInterval = 2000;

    public BannerPager(Context context) {
        this(context, null);
    }

    public BannerPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.banner_pager, this);
        mViewPager = view.findViewById(R.id.vp_banner);
    }

    public void setInterval(int interval) {
        mInterval = interval;
    }

    /**
     * 设置图片列表
     *
     * @param images 图片列表
     */
    public void setImages(List<Integer> images) {
        for (Integer image : images) {
            ImageView view = new ImageView(mContext);
            view.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT
            ));
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            view.setImageResource(image);
            view.setOnClickListener(this);
            mViewList.add(view);
            mViewPager.setAdapter(new BannerAdapter());
            mViewPager.setCurrentItem(0);
        }
    }

    @Override
    public void onClick(View v) {
        int position = mViewPager.getCurrentItem();
        if (mListener != null) {
            mListener.onBannerClick(position);
        }
    }

    /**
     * 设置横幅点击事件
     *
     * @param listener 横幅点击事件
     */
    public void setOnBannerListener(BannerClickListener listener) {
        mListener = listener;
    }

    private Handler mHandler = new Handler();

    private Runnable mScroll = new Runnable() {
        @Override
        public void run() {
            scrollToNext();
            mHandler.postDelayed(this, mInterval);
        }
    };

    /**
     * 滑动到下一张图片
     */
    public void scrollToNext() {
        int index = mViewPager.getCurrentItem() + 1;
        if (index >= mViewList.size()) {
            index = 0;
        }
        mViewPager.setCurrentItem(index);
    }

    /**
     * 开始播放图片
     */
    public void start() {
        mHandler.postDelayed(mScroll, mInterval);
    }

    /**
     * 暂停播放图片
     */
    public void stop() {
        mHandler.removeCallbacks(mScroll);
    }

    /**
     * 横幅点击监听事件
     */
    public interface BannerClickListener {
        /**
         * 横幅点击事件
         *
         * @param position 点击的横幅位置
         */
        void onBannerClick(int position);
    }

    /**
     * viewpager的适配器
     */
    private class BannerAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(mViewList.get(position));
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            return mViewList.get(position);
        }
    }
}
